import com.dinmax.flash.gsa.communication.*
import com.dinmax.flash.gsa.communication.action.*
import com.dinmax.flash.gsa.exception.*

class CommunicationEventFactory {
	
	
	public static function getComunicationEvent(doc:XML):CommunicationEvent {
		
		var name:String=getAttribute(getNode(doc.firstChild, "ACTION"), "NAME");
				
		return ActionFactory.getAction(name).execute(getNode(doc.firstChild, "ACTIONDATA"));
		
	}

	private static function getNode(node:XMLNode, name:String):XMLNode {
		
		if (node.hasChildNodes()) {
		// use firstChild to iterate through the child nodes of rootNode
		 for (var aNode:XMLNode = node.firstChild; aNode != null; aNode=aNode.nextSibling) {
				if (aNode.nodeType == 1 && aNode.nodeName==name) {
					return aNode;
				}
		  }
		  return undefined;
		}

	};

	private static function getAttribute(node:XMLNode, attribute:String):String {
		for(var sNested:String in node.attributes){
			return (node.attributes[sNested]);
		}
		return undefined;
	};
	
}